package com.scalar.chiptrack.finance.openpo.actioncommands;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.sales.reports.dao.DataDumpDao;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.io.FileWriter;
import java.io.File;

/**
 * User: ChandraSekhar
 * Date: Dec 19, 2007
 * Time: 4:42:35 PM
 */
public class PurchaseOrderSaveCSVFileCommand extends ChipTrackExtendedAction
{
    public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        String actionForward 		= null;
		HttpSession mSession 		= request.getSession();
        String fileName = "";
        String filePath;
        FileWriter fw;

        try
        {
            String poDetatils = DataDumpDao.loadPOReportDetails();
            filePath = DataDumpDao.getFileLocation("PO");
            fileName = DataDumpDao.getFileName("PO");

            fileName = fileName + "_"+ DateUtil.getTimeStamp() + ".CSV";

            if ( filePath != null && fileName != null )
            {
                boolean exists = ( new File( filePath ) ).exists();
                if ( !exists )
                {
                    File file = new File( filePath );
                    file.mkdirs();
                }
                File file1 = new File( filePath + "/" + fileName );

                fw = new FileWriter( file1 );

                try
                {
                   fw.write( poDetatils );
                }
                catch ( Exception e )
                {
                    errors.put ( "Could not write excel file to disk", null );
                    e.printStackTrace();
                }
                fw.flush();
                fw.close();

                info.put( fileName, "saved Successfully");
            }
            else
            {
                errors.put("Error", "Could not find the File path and File name");
            }


            actionForward = "Generate";
       	}
        catch ( Exception e )
        {
            errors.put( ERROR_INTERNAL_SERVER_ERROR, "Could Not save the File" );
            mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
            actionForward = "Generate";
        }
        
        //Loading the Vendors of Fab, Sort, Assembly and Test.
         request.setAttribute( "vendorsList", PurchaseOrderDAO.loadPOVendors() );

        //Loading the Purchase Order Types.
        request.setAttribute( "purchaseOrderTypesList", PurchaseOrderDAO.loadPurchaseOrderTypes() );

        request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
        
        return mapping.findForward( actionForward );
	}

}